//
//  MainViewController.swift
//  HMWeibo08
//
//  Created by 唐万超 on 15/7/28.
//  Copyright © 2015年 唐万超. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
    
     
        
    }
    
    // 当视图布局子控件的时候设置按钮
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        setComposedButton()
        
    }
    
//    // 或者在视图创建好了之后在添加，此时的tabBar已经创建好
//    override func viewDidAppear(animated: Bool) {
//        super.viewDidAppear(true)
//        
//        setComposedButton()
//    }
    
    // 设置按钮的位置
   private func setComposedButton() {
        
        // 每个tabBar的宽度
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count)
        
        // 
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        button.frame = CGRectOffset(rect, w * 2, 0)
        
    }
    
    

    // 添加所有的控制器
   private func addChildViewControllers() {
    
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
   
    // 创建子控制器
       private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        // 此处设置在全局外观对象
//        tabBar.tintColor = UIColor.orangeColor()
        
            // 设置图片
            vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        
        //nav.title = title
        
        addChildViewController(nav)
        
    }
    
    
    // 按钮的点击
    func btnClick() {
    
        let cv = UserAccount.userLogon ? ComposeViewController() : OAuthViewController()
        
        let nav = UINavigationController(rootViewController: cv)
        
        presentViewController(nav, animated: true, completion: nil)
    
    }
    
    // 复习最简单的懒加载
    //lazy var button = UIButton()
    
    // 懒加载按钮
    lazy private var button: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
         btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        // 添加到tabBar中
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
        }()
    
    


}
